package steed.ext.domain.user;

import java.util.Set;

import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.hibernate.Hibernate;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

import steed.domain.annotation.ValidateReg;
import steed.hibernatemaster.domain.BaseRelationalDatabaseDomain;
import steed.util.base.CollectionsUtil;
import steed.util.base.RegUtil;

@MappedSuperclass
public class BaseRole extends BaseRelationalDatabaseDomain {

	@ValidateReg(reg = { RegUtil.regChineseAndChar })
	protected String name;
	protected Set<Power> powerSet;
	protected String description;
	/**
	 * 是否属于系统角色(不可删除)
	 */
	protected Boolean isSystemRole;

	public BaseRole() {
		super();
	}
	
	public Boolean getIsSystemRole() {
		return isSystemRole;
	}

	public void setIsSystemRole(Boolean isSystemRole) {
		this.isSystemRole = isSystemRole;
	}

	@Transient
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToMany
	public Set<Power> getPowerSet() {
		return powerSet;
	}

	public void setPowerSet(Set<Power> powerSet) {
		this.powerSet = powerSet;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean hasPower(Power power) {
		return hasPower(power.getName());
	}

	public boolean hasPower(String powerName) {
			//TODO 超级管理员拥有所有权限
	//		if(GlobalParam.SYSTEM.SUPER_ADMIN_NAME.equals(this.name)){
	//			return true;
	//		}
			if (CollectionsUtil.isSetEmpty(powerSet)) {
				return false;
			}
			
			boolean hasPower = false;
			for (Power p : powerSet) {
				if (powerName.equals(p.getName())) {
					hasPower = true;
					break;
				}
			}
			return hasPower;
		}

	@Override
	public void initialize() {
		Hibernate.initialize(this);
	}

	@Override
	public void initializeAll() {
		initialize();
		for (Power p:getPowerSet()) {
			p.initialize();
		}
	}

}